PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Unicode Text and International Text

In addition to the string value classes described in String, Styled Text, and Text, AppleScript provides partial support for the following string types:

You can use the Unicode Text, International Text, and String classes in any script and they do not need to be enclosed in a Tell block. These classes all represent text data, though in different formats, as shown in Figure 3-1. You typically use the Unicode Text and International Text classes to get information from and send information to applications that work with these types of text.

Because the different string value classes store data in different formats, the size in bytes of a string can vary from the number of characters it contains. Comparisons between Unicode Text, International Text, and String values are not likely to be useful. You cannot determine the Length property of a value stored as Unicode Text, or access its Character, Word, or Paragraph elements (as you can with other string types, including International Text).

Figure 3-1   Formats for the String (or Text), Unicode Text, and International Text value classes

AppleScript provides limited options for displaying Unicode Text and International Text:

Figure 3-2   How the Script Editor displays String, Unicode Text, and International Text data

AppleScript provides coercions among the Unicode Text, International Text, and String (or Text) classes. For example, if your script gets a value of type Unicode Text from an application that supports Unicode, you can coerce the value to String to see it in a readable format. However, because the String, Unicode Text, and International Text classes store data differently, as shown in Figure 3-1, and because there are differences in the text data each can represent, information may be lost in some coercions. As shown in Figure 3-3, coercions from Unicode Text to International Text or String, and from International Text to String, may result in lost information. For example, the String class cannot represent 2-byte Chinese characters (from either International Text or Unicode Text values) in its 1-byte character format.

For an overview of AppleScript coercion, see Coercing Values.

The following script statements demonstrate how to initialize a string as Unicode Text and then coerce it to a standard String value:

set myString to "hello" as Unicode text
--result: «data utxt00680065006C006C006F»
myString as string
--result: "hello"

The following script retrieves text data from an open AppleWorks word processing document, adds to it, and replaces the existing text with the combined text. Because the original text is stored as Chinese characters (entered with the Chinese Language Kit), AppleWorks returns the text as International Text, so the script doesn't need to coerce it to International Text. After the script completes, the document contains the original Chinese characters, followed by "The End." in the current language and script (English Roman). If the appropriate language kit installed, AppleScript can display Chinese characters in its result window.

tell application "AppleWorks"
    -- Get text (Chinese characters) from open document.
    set myText to text body of document "Chinese Text"
    -- Add some information at the end in English.
    set myText to myText & return & "The End."
    -- Select all the current text in the document and replace it.
    select text body of document "Chinese Text"
    set selection of document "Chinese Text" to myText
end tell

If you use the & operator to combine two values of Unicode Text, the result is a list, not a string:

set greetingString to "Hello" as Unicode text
    --result: «data utxt00480065006C006C006F»
set nameString to " Bob" as Unicode text
    --result: «data utxt00200042006F0062»
set combinedString to greetingString & nameString
    --result:
    -- {«data utxt00480065006C006C006F», «data utxt00200042006F0062»}
set combinedString to (greetingString as string) ¬
                    & (nameString as string)
    --result: "Hello Bob"

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)